package brwarner.game.ui 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Brook Jensen
	 */
	public class AttackPanel extends Sprite 
	{
		//Next insertion location for buttons
		private var _nextY:Number = 0;
		
		//List of buttons
		private var _buttons:Array;
		
		//Active button
		private var _active:AttackButton = null;
		
		public function AttackPanel() 
		{
			_buttons = new Array();
		}
		
		//CLears the menu
		public function clear( ):void
		{
			_buttons.forEach( function( e:AttackButton, i:Number, a:Array ) {
				e.removeEventListener( MouseEvent.CLICK, onButtonClicked );
				removeChild(e);
			}, this );
			_buttons.splice(0);
			_nextY = 0;
		}
		
		private function onButtonClicked( e:MouseEvent ):void
		{
			if ( _active != null ) _active.Active = false;
			_active = e.target as AttackButton;
			_active.Active = true;
			dispatchEvent(new AttackPanelEvent(AttackPanelEvent.ATTACK_SELECTED, _active.ID));
		}
		
		//Gets the active id
		public function get Active( ):Number {
			if ( _active == null ) return -1;
			else return _active.ID;
		}
		
		//Gets the active id
		public function set Active( index:Number ):void {
			(_buttons[index] as AttackButton).dispatchEvent( new MouseEvent(MouseEvent.CLICK) );
		}
		
		//Inserts a button
		public function addItem( button:AttackButton ):void
		{
			//Add the button into the stack
			button.x = 0;
			button.y = _nextY;
			_nextY += button.height;
			button.addEventListener( MouseEvent.CLICK, onButtonClicked );
			addChild( button );
			_buttons.push(button);
		}
		
	}

}